<?php
namespace app\index\controller;
use app\common\controller\FrontBase;
use app\common\service\ProductCat as ProductCatService;
use app\common\service\Product as ProductService;
use think\Request;

class Product extends FrontBase{

    /**
     * 产品列表
     * @param Request $request
     * @return \think\response\View
     */
    public function index(Request $request){

        $id = $request->param('id');
        if(!intval($id)) $id = 0;
        //找到分类树作为栏目
        $productCatService = new ProductCatService();
        $productTree = $productCatService -> getMenusTree();
        $this->param['productTree'] = $productTree;

        //找到该分类下的所有产品
        $catIds = $productCatService->findAllSubCat($id);
        $catIds = array_merge($catIds,[$id]);//把自己也包含进去

        $productService = new ProductService();
        $products = $productService ->paginateByCats($catIds);

        $this->param['products'] = $products;

        //取最新的5个产品
        $newestProduct = $productService->paginate(5);
        $this->param['newsProduct'] = $newestProduct;

        return view('',$this->param);
    }


    /**
     * 产品详情
     */
    public function product(Request $request){
        $id = $request ->param('id');

        $productCatService = new ProductCatService();
        $productTree = $productCatService -> getMenusTree();
        $this->param['productTree'] = $productTree;

        $productService = new ProductService();
        //取最新的5个产品
        $newestProduct = $productService->paginate(5);
        $this->param['newsProduct'] = $newestProduct;

        $product = $productService ->find($id);
        $this->param['product'] = $product;

        $productService->addPv($id);

        return view('',$this->param);
    }
}
